<?php

include ("../lib/BLL/LopMonBLL.php");
include ("../lib/BLL/ChiTietLopMonBLL.php");
include ("../lib/BLL/QuaTrinhLopMonBLL.php");

$lopmonBLL=new LopMonBLL();
$ctlm= new ChiTietLopMonBLL();
$qllm= new QuaTrinhLopMonBLL();
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

if(isset($_POST['Luu'])){
    $somax=$_POST['somax'];
    $ma_cb=$_SESSION['ma_cb'];
    $ngaynhap=date('d/m/Y');
    $ma_lopmon=$_GET['lop'];
    $ma_ctdt="";
    $monhoc=$_POST['monhoc'];
    
    for($i=1;$i<$somax;$i++){
        $diemcc=$_POST['diem1'.$i];     
        $diemdk=$_POST['diem2'.$i];
        $ma_dkm=$_POST['ma_dkm'.$i];
        $ma_ketqua_quatrinh=$ma_lopmon.$i;
        
        if($ma_ctdt!=$_POST['ma_ctdt'.$i]){
            $ma_ctdt=$_POST['ma_ctdt'.$i];
            $ex_tieuchimon= mysql_query("  SELECT   madm_tieuchimonhoc,ma_monctdt_tieuchi
                                            FROM 	tbl_mon_ctdt,tbl_monctdt_tieuchi
                                            WHERE 	tbl_mon_ctdt.ma_mon_ctdt=tbl_monctdt_tieuchi.ma_mon_ctdt
                                            AND	    ma_ctdt='$ma_ctdt'
                                            AND     ma_monhoc='$monhoc'");
            while($row=mysql_fetch_array($ex_tieuchimon)){
                if($row['madm_tieuchimonhoc']=='cc'){
                    $ma_tieuchimon_cc=$row['ma_monctdt_tieuchi'];
                }else if($row['madm_tieuchimonhoc']=='dk'){
                    $ma_tieuchimon_dk=$row['ma_monctdt_tieuchi'];
                }    
            }
        }
        
        
        $macc=$ma_ketqua_quatrinh."cc";    
        $sql_cc="INSERT INTO tbl_ketquaquatrinh(ma_kqqt, ma_dkm, diem_kqqt, ngaytao, ma_cb,ma_monctdt_tieuchi) 
                                    VALUES  ('$macc','$ma_dkm','$diemcc','$ngaynhap','$ma_cb','$ma_tieuchimon_cc')";
        $madk=$ma_ketqua_quatrinh."dk";                                       
        $sql_dk="INSERT INTO tbl_ketquaquatrinh(ma_kqqt, ma_dkm, diem_kqqt, ngaytao, ma_cb,ma_monctdt_tieuchi) 
                                    VALUES  ('$madk','$ma_dkm','$diemdk','$ngaynhap','$ma_cb','$ma_tieuchimon_dk')";
        
        mysql_query($sql_cc);
        mysql_query($sql_dk);
        
    }// END FOR       
}



$ma_lopmon=$_GET['lop'];                            
$sql_sv_lopmon="SELECT      tbl_dangkymon.*,hoten_sv,ngaysinh_sv,ten_lop,ma_ctdt
                    FROM    tbl_dangkymon,tbl_sinhvien,tbl_nhaphoc,tbl_sv_lop,tbl_lop,tbl_khoahoc,tbl_donvi_ctdt
                    WHERE   tbl_dangkymon.ma_sv=tbl_sinhvien.ma_sv
                    AND     tbl_sinhvien.ma_sv=tbl_nhaphoc.ma_sv
                    AND     tbl_nhaphoc.ma_nhaphoc=tbl_sv_lop.ma_nhaphoc
                    AND     tbl_sv_lop.ma_lop=tbl_lop.ma_lop
                    AND     tbl_khoahoc.id_khoahoc=tbl_lop.id_khoahoc
                    AND     tbl_donvi_ctdt.id_dv_ctdt=tbl_khoahoc.id_dv_ctdt
                    AND     ngayra_nhaphoc = ''
                    AND     tbl_dangkymon.ma_lopmon='$ma_lopmon'
                    ORDER BY ten_lop,ten_sv,hoten_sv ASC";                                              
$ds_sv=null;
$ex_sv_lopmon=mysql_query($sql_sv_lopmon);
while($row=mysql_fetch_array($ex_sv_lopmon)){
    $ds_sv[]=$row;    
}
$smarty->assign('dssv',$ds_sv);

                           
$ex_check_cc=mysql_query(" SELECT  tbl_ketquaquatrinh.ma_dkm,diem_kqqt 
                            FROM    tbl_dangkymon,tbl_ketquaquatrinh,tbl_monctdt_tieuchi
                            WHERE   tbl_dangkymon.ma_dkm=tbl_ketquaquatrinh.ma_dkm
                            AND     tbl_monctdt_tieuchi.ma_monctdt_tieuchi=tbl_ketquaquatrinh.ma_monctdt_tieuchi
                            AND     ma_lopmon='$ma_lopmon'
                            AND     madm_tieuchimonhoc='cc'");


$arr_kqqt_cc=array();
$arr_kqqt_cc=null;
$arr_kqqt_dk=array();
$arr_kqqt_dk=null;


if(mysql_num_rows($ex_check_cc)>0){
    while($row=mysql_fetch_array($ex_check_cc)){
        $key=$row['ma_dkm'];
        $arr_kqqt_cc[$key]=$row['diem_kqqt'];    
    }
    
        $ex_check_dk=mysql_query(" SELECT  tbl_ketquaquatrinh.ma_dkm,diem_kqqt 
                                    FROM    tbl_dangkymon,tbl_ketquaquatrinh,tbl_monctdt_tieuchi
                                    WHERE   tbl_dangkymon.ma_dkm=tbl_ketquaquatrinh.ma_dkm
                                    AND     tbl_monctdt_tieuchi.ma_monctdt_tieuchi=tbl_ketquaquatrinh.ma_monctdt_tieuchi
                                    AND     ma_lopmon='$ma_lopmon'
                                    AND     madm_tieuchimonhoc='dk'");
        
        if(mysql_num_rows($ex_check_dk)>0){
            while($row=mysql_fetch_array($ex_check_dk)){
                $key=$row['ma_dkm'];
                $arr_kqqt_dk[$key]=$row['diem_kqqt'];    
            }
        }
}

$smarty->assign('arr_kqqt_cc',$arr_kqqt_cc);
$smarty->assign('arr_kqqt_dk',$arr_kqqt_dk);




$sql_mon=   "   SELECT  DISTINCT madm_mh,ten_mh 
                FROM    dm_monhoc,tbl_mon_ctdt,tbl_lopmon_monctdt
                WHERE   dm_monhoc.madm_mh=tbl_mon_ctdt.ma_monhoc
                AND     tbl_lopmon_monctdt.ma_mon_ctdt=tbl_mon_ctdt.ma_mon_ctdt
                AND     ma_lopmon='$ma_lopmon'
                LIMIT 1";
$kq_mon=mysql_fetch_array(mysql_query($sql_mon));    
$smarty->assign('monhoc',$kq_mon);


$sql_nganh="   SELECT 	DISTINCT ten_nganh 
                FROM 	tbl_lopmon_monctdt,tbl_mon_ctdt,tbl_ctdt,dm_nganh
                WHERE 	tbl_lopmon_monctdt.ma_mon_ctdt=tbl_mon_ctdt.ma_mon_ctdt
                AND	    tbl_ctdt.ma_ctdt=tbl_mon_ctdt.ma_ctdt
                AND	    tbl_ctdt.ma_ctdt=tbl_mon_ctdt.ma_ctdt
                AND	    tbl_ctdt.madm_nganh=dm_nganh.madm_nganh
                AND     ma_lopmon='$ma_lopmon'";

$nganh_lopmon=null;
$ex_nganhlopmon=mysql_query($sql_nganh);
while($row=mysql_fetch_array($ex_nganhlopmon)){
    $nganh_lopmon.=$row['ten_nganh'].", ";    
}
$smarty->assign('nganh_lopmon',$nganh_lopmon);



//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$smarty->display('diem/add_diemdk.tpl');   
?>












